package cn.net.yugu.doraemon.datahub.core;

import cn.net.yugu.doraemon.datahub.config.DataHubProperties;
import cn.net.yugu.doraemon.datahub.worker.RecordConsumeWorker;
import com.aliyun.datahub.client.DatahubClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

/**
 * @author wanghongli
 * @date 2024/5/30 20:14
 * @description DataHubConsumeHandler
 **/
public class DataHubConsumeHandler implements InitializingBean, DisposableBean {
    @Autowired
    private DataHubProperties properties;
    @Autowired
    private ApplicationContext context;
    private final DatahubClient datahubClient;

    public DataHubConsumeHandler(DatahubClient datahubClient) {
        this.datahubClient = datahubClient;
    }

    @Override
    public void destroy() throws Exception {
        RecordConsumeWorker.shutdown();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        /**
         * 初始化Worker线程
         */
        RecordConsumeWorker.initWorkerResource(this.datahubClient, context, properties);
        /**
         * 启动Worker线程
         */
        RecordConsumeWorker.start();
    }
}
